package com.xq.tmall.service.impl;

import com.xq.tmall.dao.AddressMapper;
import com.xq.tmall.dao.DetailaddressDao;
import com.xq.tmall.entity.Address;
import com.xq.tmall.entity.Detailaddress;
import com.xq.tmall.service.DetailaddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Detailaddress)表服务实现类
 *
 * @author makejava
 * @since 2022-01-27 16:19:49
 */
@Service("detailaddressService")
public class DetailaddressServiceImpl implements DetailaddressService {
    @Resource
    private DetailaddressDao detailaddressDao;
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Detailaddress queryById(Integer id) {
        return this.detailaddressDao.queryById(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public List<Detailaddress> queryListByUserId(Integer userId) {
        List<Detailaddress> list = this.detailaddressDao.queryByUserId(userId);
        if(list.size()==0){
            return new ArrayList<>();
        }



        List<Address> addresses=addressMapper.queryAddress(list.stream().map(Detailaddress::getAddressId).collect(Collectors.toList()));
        Map<String, String> collect = addresses.stream().collect(Collectors.toMap(Address::getAddress_areaId, e -> e.getAddress_name()));
        list.forEach(e-> e.setDetailAddress(collect.get(e.getAddressId().toString())+e.getDetailAddress()));
        return list;
    }

    public String get(Integer s){
        return s.toString();
    }


    @Override
    public List<Detailaddress> queryByUserId(Integer userId) {
        return this.detailaddressDao.queryByUserId(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Detailaddress> queryAllByLimit(int offset, int limit) {
        return this.detailaddressDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param detailaddress 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(Detailaddress detailaddress) {
        return detailaddressDao.insert(detailaddress)>0;
    }

    /**
     * 修改数据
     *
     * @param detailaddress 实例对象
     * @return 实例对象
     */
    @Override
    public Detailaddress update(Detailaddress detailaddress) {
        this.detailaddressDao.update(detailaddress);
        return this.queryById(detailaddress.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.detailaddressDao.deleteById(id) > 0;
    }
}
